﻿
@{
    Layout = null;
}
<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<div style="margin-top:20px;margin-left:20px;">
    客户编码：
    <input type="text" id="CustomerCode" style="width:200px;height:30px;" />
    客户名称：
    <input type="text" id="CustomerName" style="width:200px;height:30px;" />


    <button type="button" class="layui-btn" id="intPersonInfo">查询</button>
    <button type="button" class="layui-btn" lay-on="iframe">添加</button>
</div>

<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <button class="layui-btn layui-btn-sm  layui-bg-radius" style="background-color:orange" lay-event="edit">修改</button>
        <button class="layui-btn layui-btn-sm  layui-bg-radius" style="background-color:red" lay-event="del">删除</button>
    </div>
</script>

<div style="margin-top:20px;margin-left:20px">
    <table id="demo" lay-filter="test"></table>
</div>
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script>

    layui.use(['table'], function () {
        var table = layui.table;
        var util = layui.util;

        //查询
        $('#intPersonInfo').on('click', function () {
            var customerCode = $('#CustomerCode').val();
            var customerName = $('#CustomerName').val();
            table.reload('demo', {
                where: {
                    customerCode: customerCode,
                    customerName: customerName,
                }
            });
        })
        // 创建渲染实例
        function intPersonInfo() {
            table.render({
                elem: '#demo',
                url: '/CustomerManagement/GetCustomerManagementInfo',
                page: true,
                limit: 5,
                limits: [5, 10, 15],
                cols: [[
                    { field: 'customerCode', width: 180, title: '客户编码 ', align: 'center', sort: true },
                    { field: 'customerName', width: 180, title: '客户名称', align: 'center' },
                    { field: 'customerAbbreviation', width: 180, title: '客户简称', align: 'center' },
                    {
                        field: 'customerType', width: 180, align: 'center', title: '客户类型', templet: function (row) {
                            var temcus = row.customerType == 1? "企业" : "个人";
                            return temcus;
                        },
                    },
                    { field: 'customerPhone', width: 180, title: '客户电话', align: 'center' },
                    { field: 'contactName', width: 180, title: '联系人', align: 'center' },
                    { field: 'contactPhone', width: 170, title: '联系人-电话', align: 'center' },
                    {
                        field: 'state', width: 180, align: 'center', title: '是否启用', templet: function (row) {
                            var state = row.state == 1 ? "启用" : "禁用";
                            return state;
                        },
                    },
                    { field: '操作', width: 180, title: '操作', toolbar: "#barDemo", align: 'center' },
                ]],
            });
        }

        intPersonInfo()

        util.on('lay-on', {
            iframe: function () {
                // iframe 层
                layer.open({
                    type: 2,
                    title: "添加客户信息",
                    shade: [0],
                    area: ['960px', '500px'],//弹出层的宽度和高度
                    offset: ['100px', '200px'],//弹出层的上边距和左边距
                    shift: 0.8,
                    content: ['/CustomerManagement/Insert'], //iframe的url，no代表不显示滚动条
                    end: function () {
                        intPersonInfo();
                    }
                })
            },
        });
        
       table.on('tool(test)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    $.ajax({
                        url: "/CustomerManagement/DeleteCustomerManagementInfo",
                        data: {
                            customerId: data.id,
                        },
                        type: "get",
                        datatype: "json",
                        success: function (res) {
                            if(res.code>0){
                                layer.msg('删除成功');
                                intPersonInfo();
                            }
                            else{
                                layer.msg('删除失败:'+res.msg);
                            }
                        }
                    })
                    layer.close(index); //关闭弹框
                })
            }
            else if(layEvent === 'edit'){
                layer.open({
                    type: 2,
                    title: "修改客户信息",
                    shade: [0],
                    area: ['960px', '500px'],//弹出层的宽度和高度
                    offset: ['100px', '200px'],//弹出层的上边距和左边距
                    shift: 0.8,
                    content: ['/CustomerManagement/Edit?id='+data.id,'no']
                })
            }
       })
    })
</script>